<template>
	<view class="attendance" v-if="Object.keys(baseinfo).length > 0">
		<view class="baseinfo">
			<view class="left">
				<image v-if="baseinfo.face" :src="baseinfo.face" mode=""></image>
				<image v-else src="../../../static/image/mine/avatar.png" mode=""></image>
			</view>
			<view class="center">
				<view class="name">
					<text>{{baseinfo.name}}</text>
					<text>{{baseinfo.mobile}}</text>
				</view>
				<view class="right">
					<view class="age">
						<text>年龄</text>
						<text>{{baseinfo.age}}</text>
					</view>
					<view class="age native_place">
						<text>籍贯</text>
						<text class="address">{{baseinfo.address}}</text>
					</view>
				</view>
				<view class="idcard">
					<image src="../../../static/image/idcard.png" mode=""></image>
					<text>{{baseinfo.idcard}}</text>
				</view>
			</view>
			<!-- <view class="line"></view> -->
			<!-- <view class="right">
				<view class="age">
					<text>年龄</text>
					<text>{{baseinfo.age}}</text>
				</view>
				<view class="age native_place">
					<text>籍贯</text>
					<text class="address">{{baseinfo.address}}</text>
				</view>
			</view> -->
		</view>
		<view class="date">
			<view class="title">
				<text></text>
				<text>考勤记录</text>
			</view>
			<calendar name="calendar" :showDot="true" :extraData="extraData" :count="count" @calendarTap="calendarTap"
				@monthTap="monthTap" />
			<view class="dotted_line">
				<view></view>
			</view>
			<view class="project">
				<image src="../../../static/image/location.png" mode=""></image>
				<text>所在项目：{{project}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	import api from '@/static/js/api.js'

	import calendar from './tale-calendar/calendar.vue';

	export default {
		components: {
			calendar
		},
		data() {
			return {
				id: '',
				project: '',
				extraData: [], //{date: '2021-12-3',dot: true,active: true},
				start_date: '',
				end_date: '',
				current_start_date: '',
				current_end_date: '',
				baseinfo: {},
				count: 0, //打卡总天数
				all_dates: []
			}
		},
		onLoad(option) {
			this.id = option.id
			this.project = option.project
		},
		onShow() {
			if (uni.getStorageSync('userInfo')) {
				this.userInfo = JSON.parse(uni.getStorageSync('userInfo'));
				var d = new Date()
				this.current_start_date = d.getFullYear() + '-' + this.zeroFilling(d.getMonth() + 1) + '-' + '01'
				var d1 = new Date(d.getFullYear(), d.getMonth() + 1, 0)
				this.current_end_date = d1.getFullYear() + '-' + this.zeroFilling(d1.getMonth() + 1) + '-' + d1.getDate()
				this.getdata(this.current_start_date, this.current_end_date)
			}
		},
		methods: {
			getdata(current_start_date, current_end_date) {
				// console.log(this.getNowM(current_start_date));
				this.getNowM(current_start_date).forEach(item => {
					this.extraData.push({
						date: item,
						dot: true,
						active: false
					})
				})
				uni.showLoading({
					title: '正在加载...',
					mask: true
				})
				api.workerAtt('res.partner', '_work_attendance_info', this.userInfo.access_token, this.userInfo.uid, this
					.id, current_start_date, current_end_date).then(
					res => {
						this.baseinfo = res.data
						this.count = res.data.attendance.length
						uni.hideLoading()
						res.data.attendance.forEach(item => {
							this.extraData.forEach(i => {
								if (i.date == item) {
									i.active = true
								}
							})
						})
					})
			},
			//点击日期
			calendarTap(e) {
				console.log(e);
			},
			zeroFilling(val) {
				if (val > 9) {
					return val
				} else {
					return '0' + val
				}
			},
			//选择年月  月份+1  补0
			monthTap(val) {
				this.start_date = val.year + '-' + this.zeroFilling(val.month + 1) + '-' + '01'
				var d = new Date(val.year, val.month + 1, 0)
				this.end_date = d.getFullYear() + '-' + this.zeroFilling(d.getMonth() + 1) + '-' + d.getDate()
				this.getdata(this.start_date, this.end_date)
			},
			// 格式化日期
			formatDate(date) {
				date = new Date(date);
				let myyear = date.getFullYear();
				let mymonth = date.getMonth() + 1;
				let myweekday = date.getDate();
				mymonth < 10 ? mymonth = "0" + mymonth : mymonth;
				myweekday < 10 ? myweekday = "0" + myweekday : myweekday;
				return `${myyear}-${mymonth}-${myweekday}`;
			},
			// 获取当前月的天数
			mGetDate(e) {
				var date = new Date(e);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				var d = new Date(year, month, 0);
				return d.getDate();
			},
			// 获取当前月的所有日期
			getNowM(e) {
				let now = new Date(e);
				// console.log(e);
				if (e.split('-')[0] > (new Date()).getFullYear()) {
					return []
				} else {
					if (e.split('-')[1] == (new Date()).getMonth() + 1) {
						var current_month_num = (new Date()).getDate()
						let current_month = [];
						for (let i = 1; i <= current_month_num; i++) {
							let day = now.setDate(i);
							let everyDay = this.formatDate(day);
							current_month.push(everyDay);
						}
						return current_month;
					} else {
						var current_month_num = this.mGetDate(e);
						let current_month = [];
						for (let i = 1; i <= current_month_num; i++) {
							let day = now.setDate(i);
							let everyDay = this.formatDate(day);
							current_month.push(everyDay);
						}
						return current_month;
					}
				}
				//  let current_month_num = this.mGetDate(e);
				// // let current_month_num = (new Date()).getDate()
				//  let current_month = [];
				//  for (let i = 1; i <= current_month_num; i++) {
				//    let day = now.setDate(i);
				//    let everyDay = this.formatDate(day);
				//    current_month.push(everyDay);
				//  }
				//  return current_month;
			}
		},
	}
</script>

<style lang="less" scoped>
	.attendance {
		width: 100vw;
		height: 100vh;
		background-color: #F5F5F9;
		padding: 20rpx 32rpx 0;

		.baseinfo {
			padding: 50rpx 0 50rpx 40rpx;
			background: #fff;
			display: flex;
			align-items: center;
			box-shadow: 0px 4rpx 28rpx 0px #EBEBF5;
			border-radius: 10rpx;

			.left {
				width: 100rpx;
				height: 100rpx;

				image {
					width: 100%;
					height: 100%;
					border-radius: 50%;
				}
			}

			.center {
				margin-left: 50rpx;

				.name {
					display: flex;
					align-items: center;

					text:first-child {
						font-size: 36rpx;
						font-weight: 500;
						color: #242A36;
					}

					text:last-child {
						font-size: 24rpx;
						color: #676C71;
						margin-left: 18rpx;
					}
				}

				.idcard {
					display: flex;
					align-items: center;
					margin-top: 10rpx;

					image {
						width: 42rpx;
						height: 32rpx;
					}

					text {
						font-size: 28rpx;
						color: #676C71;
						margin-left: 18rpx;
					}
				}
			}

			.line {
				width: 2rpx;
				height: 70rpx;
				border: 2rpx solid #E1E1E1;
				margin-left: 10rpx;
				margin-right: 10rpx;
			}

			.right {
				display: flex;
				margin-top: 10rpx;

				.age {
					font-size: 28rpx;

					text:first-child {
						color: #999999;
					}

					text:last-child {
						color: #333333;
						margin-left: 20rpx;
					}
					
					.address {
					}
				}

				.native_place {
					margin-left: 30rpx;
				}
			}
		}

		.date {
			background: #fff;
			margin-top: 16rpx;
			padding-top: 26rpx;
			padding-bottom: 32rpx;
			box-shadow: 0px 4rpx 28rpx 0px #EBEBF5;
			border-radius: 10rpx;

			.title {
				display: flex;
				align-items: center;

				text:first-child {
					width: 8rpx;
					height: 38rpx;
					background: #E8002A;
					display: inline-block;
					margin-left: 36rpx;
				}

				text:last-child {
					font-size: 36rpx;
					color: #333333;
					margin-left: 20rpx;
				}
			}

			.dotted_line {
				padding: 18rpx;

				view {
					height: 2rpx;
					border: 2rpx dotted #D6D6D6;
				}
			}

			.project {
				display: flex;
				align-items: center;
				padding: 0 32rpx;

				image {
					width: 24rpx;
					height: 32rpx;
					margin-right: 12rpx;
				}

				text {
					font-size: 28rpx;
					color: #999999;
					flex: 1;
				}
			}
		}
	}
</style>
